package com.fdb.efp.console.api.rest;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.fdb.basic.framework.core.constant.PubConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fdb.efp.console.service.facade.ContTempInfoService;
import com.fdb.efp.console.service.vo.ContTempGVO;
import com.fdb.efp.console.service.vo.ContTempInfoVO;
import com.fdb.efp.console.service.vo.ContTempLinkGVO;
import com.fdb.efp.console.service.facade.SRoleDataRuleService;
import com.fdb.basic.framework.core.constant.ApplicationConstance;
import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class ContTempInfoController extends AbstractController  {
	
	@Autowired
	@Qualifier("contTempInfoService")
	private ContTempInfoService contTempInfoService;
	
	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;
	
	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/cont/temp/infos", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<ContTempInfoVO>> queryContTempInfoAll(
			@RequestBody ContTempInfoVO vo) {		
		// 根据用户角色获取用户权限方案
		List<ContTempInfoVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = contTempInfoService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = contTempInfoService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = contTempInfoService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = contTempInfoService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);
		
		
	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/cont/temp/info/{contTempId}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<ContTempInfoVO> queryByPk(@PathVariable("contTempId") String contTempId) {		
		ContTempInfoVO inVo = new ContTempInfoVO();
				inVo.setContTempId(contTempId);
		ContTempInfoVO outputVo = contTempInfoService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param contTempInfo
	 * @return
	 */
	@RequestMapping(value="/cont/temp/info", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody ContTempInfoVO inContTempInfoVo) {		
		int outputVo = contTempInfoService.deleteByPk(inContTempInfoVo);
		if ( outputVo == -2 ) {
			ResponseData<Integer> responseData = new ResponseData<Integer>();
			responseData.setRows(outputVo);
			responseData.setMessage("只能删除待生效状态的合同模版");
			responseData.setCode(ApplicationConstance.FAILURE);
			responseData.setSuccess(false);
			return responseData;
		}else
			return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inContTempInfoVo
	 * @return
	 */
	@RequestMapping(value="/cont/temp/info", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody ContTempInfoVO inContTempInfoVo) {		
		setUserInfoToVO(inContTempInfoVo);
		int outputVo = contTempInfoService.updateByPk(inContTempInfoVo);
		if (outputVo < 0) {
			String message;
			if(outputVo == -2) {
				message = "不能存在 模板种类, 合同类型, 是否最高额合同, 合同模板存储路径四个维度一致的合同模版!";
			}else{
				message = "处理失败";
			}
			return getFailuerMsg(message, outputVo);
		}else {
			return getResponseData(outputVo);
		}
	}
	
	/**
	 * 新增数据
	 * @param inContTempInfoVo
	 * @return
	 */
	@RequestMapping(value="/cont/temp/info", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertContTempInfo(@RequestBody ContTempInfoVO inContTempInfoVo) {
		setUserInfoToVO(inContTempInfoVo);
		int outputVo = contTempInfoService.insertContTempInfo(inContTempInfoVo);
		if (outputVo < 0) {
			String message;
			if(outputVo == -2) {
				message = "不能存在 模板种类, 合同类型, 是否最高额合同, 合同模板存储路径四个维度一致的合同模版!";
			}else {
				message = "处理失败";
			}
			return getFailuerMsg(message, outputVo);
		}else {
			return getResponseData(outputVo);
		}
	}
	
	/**
	 * 插入合同模版， 并且生效
	 * @param inContTempInfoVo
	 * @return
	 */
	@RequestMapping(value="/cont/temp/info/submit", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertSubmitContTemp(@RequestBody ContTempInfoVO inContTempInfoVo){
		setUserInfoToVO(inContTempInfoVo);
		int outputVo = contTempInfoService.insertSubmitContTemp(inContTempInfoVo);
		if (outputVo < 0) {
			String message;
			if(outputVo == -2) {
				message = "不能存在 模板种类, 合同类型, 是否最高额合同, 合同模板存储路径四个维度一致的合同模版!";
			}else {
				message = "处理失败";
			}
			return getFailuerMsg(message, outputVo);
		}else
			return getResponseData(outputVo);
	}
	
	/**
	 * 更新合同模版， 并且生效
	 * @param inContTempInfoVo
	 * @return
	 */
	@RequestMapping(value="/cont/temp/info/submit", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateSubmitContTemp(@RequestBody ContTempInfoVO inContTempInfoVo){
		setUserInfoToVO(inContTempInfoVo);
		int outputVo = contTempInfoService.updateSubmitContTemp(inContTempInfoVo);
		if (outputVo < 0) {
			String message;
			if(outputVo == -2) {
				message = "不能存在 模板种类, 合同类型, 是否最高额合同, 合同模板存储路径四个维度一致的合同模版!";
			}else {
				message = "处理失败";
			}
			return getFailuerMsg(message, outputVo);
		}else
			return getResponseData(outputVo);
	}
	
	/**
	 * 使合同模版失效
	 * @param inContTempInfoVo
	 * @return
	 */
	@RequestMapping(value="/cont/temp/info/disable", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> disableContTemp(@RequestBody ContTempInfoVO inContTempInfoVo){
		int outputVo = contTempInfoService.disableContTemp(inContTempInfoVo);
		if (outputVo < 0) {
			return getFailuerMsg("存在有生效的合同模板组引用该合同模板,不可失效该合同模版。", outputVo);
		}else
			return getResponseData(outputVo);
	}
	
	/**
	 * 根据 合同模版组ID 查询关联的合同模版
	 * @param contTempGVO
	 * @return
	 */
	@RequestMapping(value="/cont/temp/info/contGLinked", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<ContTempInfoVO>> queryLinkedGontTempInfos(
			@RequestBody ContTempGVO contTempGVO) {
		List<ContTempInfoVO> outputVo = contTempInfoService.queryLinkedGontTempInfos(contTempGVO);
		return getResponseData(outputVo);
	}
	
	/**
	 * 查询当前 生效的合同模版
	 * @param contTempGVO
	 * @return
	 */
	@RequestMapping(value="/cont/temp/info/allEnables", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<ContTempInfoVO>> queryAllEnable(
			@RequestBody ContTempInfoVO contTempGVO) {
		// 根据用户角色获取用户权限方案
		List<ContTempInfoVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		contTempGVO.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(contTempGVO);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = contTempInfoService.queryAllOwner(contTempGVO);
			break;
		case CurrOrg:
			outputVo = contTempInfoService.queryAllCurrOrg(contTempGVO);
			break;
		case CurrDownOrg:
			outputVo = contTempInfoService.queryAllCurrDownOrg(contTempGVO);
			break;
		default:
			outputVo = contTempInfoService.queryAllOwner(contTempGVO);
			break;
		}
		return getResponseData(outputVo);
	}
	
	/**
	 * 查询合同模版组下, 未关联合同模版组和生效的合同模版
	 * @param contTempLinkGVO
	 * @return
	 */
	@RequestMapping(value="/cont/temp/info/notlink/enable", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<ContTempInfoVO>> queryNotLinkEnable(
			@RequestBody ContTempLinkGVO contTempLinkGVO) {
		setUserInfoToVO(contTempLinkGVO);
		List<ContTempInfoVO> outputVo = contTempInfoService.queryNotLinkEnable(contTempLinkGVO);
		return getResponseData(outputVo);
	}
	
	/**
	 * 下载 合同模版 文件
	 */
	@RequestMapping(value="/cont/temp/info/download", method=RequestMethod.GET)
	public void downloadContractTempFile( @RequestParam("fileName") String fileName, 
			HttpServletResponse response) {
		/** 【暂时不需要到的方法】
		response.setCharacterEncoding("UTF-8");
	    response.setContentType("multipart/form-data");
	    response.setHeader("Content-disposition",
	            String.format("attachment; filename=\"%s\"", fileName));
	    FileByteInfoVO file = contractDocFileService.queryContractDocFileByName(fileName);
    	long fileLength = file.getFileBytes() != null ? file.getFileBytes().length : 0;
    	response.setHeader("Content-Length", String.valueOf(fileLength));
    	OutputStream out = null;
    	try {
    		out = response.getOutputStream();
    		byte[] buff = file.getFileBytes();
    		
    		out.write(buff);
    		out.flush();
    		
    	} catch (IOException e) {
    		e.printStackTrace();
    	}finally {
			if ( out != null ) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}*/
	}
	
	/**
	 * 获得失败的返回
	 * @param message
	 * @param outputVo
	 * @return
	 */
	private ResponseData<Integer> getFailuerMsg(String message, Integer outputVo){
		ResponseData<Integer> responseData = new ResponseData<Integer>();
		responseData.setRows(outputVo);
		responseData.setMessage(message);
		responseData.setCode(ApplicationConstance.FAILURE);
		responseData.setSuccess(false);
		return responseData;
	}
}
